package com.yao.system.controller;


import com.yao.system.annotation.MyAnnotation;
import com.yao.system.entity.Result;
import com.yao.system.entity.User;
import com.yao.system.entity.dto.CompanyUserDTO;
import com.yao.system.entity.dto.UserDTO;
import com.yao.system.entity.dto.UserImage;
import com.yao.system.service.IUserService;
import com.yao.system.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @MyAnnotation
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @MyAnnotation
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    @MyAnnotation
    @PostMapping("/companyRegister")
    public Result companyRegister(@RequestBody CompanyUserDTO dto) {
        return userService.companyRegister(dto);
    }

    /**
     * 修改用户信息（账号密码）
     * @param user
     * @return
     */
    @MyAnnotation
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    /**
     * 获取用户信息
     * @return
     */
    @MyAnnotation
    @GetMapping("getUserInfo")
    public Result getUserInfo(){
        return userService.getUserInfo();
    }

    /**
     * 保存用户头像
     * @param userImage
     * @return
     */
    @MyAnnotation
    @PostMapping("/uploadImg")
    public Result saveImage(@RequestBody UserImage userImage){
        String imageUrl = userImage.getImageUrl();
        System.out.println(imageUrl);
        UserDTO user = UserContext.getUser();
        User user1 = new User();
        user1.setId(user.getId());
        user1.setImage(imageUrl);
        userService.updateById(user1);
        return Result.ok(imageUrl,"保存成功");
    }

    /**
     * 管理员查询用户列表
     */
    @MyAnnotation
    @GetMapping("/admin-list/{page}/{size}")
    public Result queryUserList(@PathVariable("page")Integer page,
                                @PathVariable("size")Integer size){
        return userService.queryUserList(page,size);
    }

    /**
     * 管理员修改用户信息
     */
    @MyAnnotation
    @PostMapping("/admin-update")
    public Result updateUserAdmin(@RequestBody User user){
        return userService.updateUserAdmin(user);
    }

    /**
     * 管理员删除用户
     */
    @MyAnnotation
    @PostMapping("/admin-delete")
    public Result deleteUserAdmin(@RequestBody User user){
        userService.removeById(user);
        return Result.ok("删除成功");
    }

}
